﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Security.Permissions;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

using Swaf;
using Swaf.Gui;
using Swaf.Gui.AspNet2;

[assembly: WebResource("ScsAspNet.Syrinx.Gui.AspNet.Ajax.script.GoogleMapGlobalScript.js", "application/x-javascript")]
namespace Syrinx.Gui.AspNet.Ajax
{
	public class NitobiGoogleMapBizObjListDisplay : GoogleMap.Gui.AspNet.GoogleMapBizObjListDisplay, IAjaxClientControl
	{
		private bool m_myPushPinSelectEvent = false;
		public NitobiGoogleMapBizObjListDisplay() : base() { }
		public NitobiGoogleMapBizObjListDisplay(ActiveWidget widget) : base(widget) { }

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			string scriptUrl = Page.ClientScript.GetWebResourceUrl(typeof(NitobiGoogleMapBizObjListDisplay), "ScsAspNet.Syrinx.Gui.AspNet.Ajax.script.GoogleMapGlobalScript.js");
			Page.ClientScript.RegisterClientScriptInclude("SyrinxNitGoogleMap", scriptUrl);

			string setupScript = AssRez.Instance.getRezString("Syrinx.Gui.AspNet.Ajax.script.GoogleMapSetupScript.js");
			Hashtable m = new Hashtable();
			m["ClientId"] = ClientID;
			m["BizProcId"] = BizProc.BizProc.Id;
			setupScript = Swaf.Application.globalApp.resMgr.resolve(setupScript, m);
			Page.ClientScript.RegisterStartupScript(typeof(NitobiGoogleMapBizObjListDisplay), "Setup" + WidgetId, setupScript, true);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <remarks>If the usage of this control on an asp.net page doesnt set the contentname for a custom google map template,
		/// this class must override the UserControl behavior of defaulting to the class name.  In this case, we still want to use
		/// the base GoogleMapBizObjListDisplay template, which only calls getHtml</remarks>
		public override string ContentName
		{
			get
			{
				string s = base.ContentName;
				if (s.CompareTo("Syrinx.Gui.AspNet.Ajax.NitobiGoogleMapBizObjListDisplay") == 0)
					return "GoogleMap.Gui.AspNet.GoogleMapBizObjListDisplay";
				return s;
			}
			set
			{
				base.ContentName = value;
			}
		}

		//public override void showBizObjs(IBusinessProcess bizproc, Swaf.BizObj.IBizObjCursor list)
		//{
		//    if (!(Page is INitobiPage))
		//        base.showBizObjs(bizproc,list);
		//}

		protected ActiveBizProc BizProc
		{
			get
			{
				if (m_activeBizProc == null)
				{
					string parentBizProcId = null;
					if (State.ParentBizProcIds != null && State.ParentBizProcIds.Count > 0)
						parentBizProcId = State.ParentBizProcIds[0];
					if (parentBizProcId != null)
						m_activeBizProc = BPC.getActiveBizProc(parentBizProcId);
					if (m_activeBizProc == null)
						throw new BaseException(string.Format("Cannot find parent bizproc for bizObjListDisplay {0} using parent id of {1}", WidgetId, parentBizProcId));
				}
				return m_activeBizProc;
			}
		}
		private ActiveBizProc m_activeBizProc = null;

		protected override bool shouldPositionOnFirst()
		{
			return !((INitobiPage)Page).IsAjaxEvent;
		}

		#region IAjaxClientControl Members

		bool m_createActiveOnLoad = true;
		public bool CreateOnLoad { set { m_createActiveOnLoad = value; } }

		public void setState(XmlElement clientState)
		{
		}

		public void getState(XmlElement clientState, string pageHtml)
		{
			if (!m_myPushPinSelectEvent)
			{
				StringBuilder buff = new StringBuilder(1024);
				buff.Append("var me = $('#").Append(ClientID).Append("map');");
				clientState.SetAttribute("script", getMapPointsScript(buff));
			}
		}

		public void processAjaxEvent(XmlElement eventInfo)
		{
			if (eventInfo.GetAttribute("event").CompareTo("bizObjSelected") == 0)
			{
				m_myPushPinSelectEvent = true;
				string bizObjId = eventInfo.GetAttribute("value");
				ActiveBizProc bizproc = BPC.getActiveBizProc(State.ParentBizProcIds[0]);
				bizproc.BizProc.selectBizObj(bizObjId, this);
			}
		}

		public bool NeedToGenerateHtmlWithServerControls
		{
			get { return false; }
		}

		#endregion

	}
	[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
	[PersistenceModeAttribute(PersistenceMode.InnerProperty)]
	[Serializable]
	public class PushPin : GoogleMap.Gui.AspNet.PushPin { }
}
